package com.csweimei.rentmanagementtreasure.pop;

import android.content.Context;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.csweimei.rentmanagementtreasure.R;
import com.github.mikephil.charting.formatter.IFillFormatter;
import com.gtdev5.geetolsdk.mylibrary.util.LogUtils;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

public class PopCustomize extends BottomPopupView {

    private Context mContext;
    private TextView cancel,sure,title;
    private PopSelClick mPopSelClick;

    private RecyclerView recyclerView;
    private String sel;


    public PopCustomize(@NonNull Context context,PopSelClick popSelClick) {
        super(context);
        mContext = context;
        mPopSelClick = popSelClick;

    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_customize;
    }

    private List<PopBean> mList = new ArrayList<>();
    private List<PopBean> mListNew = new ArrayList<>();
    public void setData(List<PopBean> list){
        mListNew.addAll(list);
        mList.clear();
        list.add(new PopBean("+自定义"));
        mList.addAll(list);
    }

    private String titleT;
    public void setTitle(String s){
        titleT = s;
    }


    @Override
    protected void onCreate() {
        super.onCreate();
        findViewById(R.id.cancel).setOnClickListener(v -> {
            dismiss();
        });

        findViewById(R.id.sure).setOnClickListener(v -> {
            mPopSelClick.sel(new PopBean(sel));
            dismiss();
        });

        title = findViewById(R.id.title);
        title.setText(titleT);


        recyclerView = findViewById(R.id.recycler);
        Adapter adapter = new Adapter();
        recyclerView.setAdapter(adapter);
        adapter.setList(mList);

        adapter.addChildClickViewIds(R.id.name);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            if (mList.get(position).getName().equals("+自定义")){
                ToastUtils.showShort("点击了自定义");
                PopInput popInput = new PopInput(mContext, new PopSelClick() {
                    @Override
                    public void sel(PopBean x) {
                        mList.clear();
                        mListNew.add(new PopBean(x.name));
                        mListNew.add(new PopBean("+自定义"));
                        mList.addAll(mListNew);
                        adapter.setList(mList);
                    }
                });
                PopUtils.showPop(mContext,popInput,false);

            }else {
                adapter.multipleChoose(position);
                LogUtils.e(adapter.getData().toString());
                StringBuffer sb = new StringBuffer();
                for (PopBean p : adapter.getData()) {
                    if (p.isCheck){
                        sb.append(p.name).append(",");
                    }
                }
                sel = sb.toString();
            }



        });

    }


    class Adapter extends BaseQuickAdapter<PopBean, BaseViewHolder>{

        public Adapter() {
            super(R.layout.item_quarter);
        }

        //设置多选。选中取消
        public void multipleChoose(int position) {
            PopBean item = getItem(position);

            if (!item.getName().equals("+自定义")){
                if (item.isCheck()) {
                    item.setCheck(false);  //内置一个选择状态字段
                } else {
                    item.setCheck(true);

                }
            }

            notifyDataSetChanged();  //更新数据源,在这里更新之后,activity里面的数据也会同步更新

        }


        @Override
        protected void convert(@NonNull BaseViewHolder baseViewHolder, PopBean s) {
            baseViewHolder.setText(R.id.name,s.getName());
            baseViewHolder.setBackgroundResource(R.id.name,R.drawable.rect_f6_60);

            if (s.isCheck){
                baseViewHolder.setTextColorRes(R.id.name,R.color.main_c);
                baseViewHolder.setBackgroundResource(R.id.name,R.drawable.rect_f6_60_s);
            }else {
                baseViewHolder.setTextColorRes(R.id.name,R.color.text_c);
                baseViewHolder.setBackgroundResource(R.id.name,R.drawable.rect_f6_60);
            }



        }




    }

}
